package com.okbihuo.perm.component.file.rest;


import com.okbihuo.perm.component.file.config.FileConfig;
import com.okbihuo.perm.component.file.domain.ComFile;
import com.okbihuo.perm.component.file.logic.FileLogic;
import com.okbihuo.perm.component.file.service.ComFileService;
import com.okbihuo.perm.component.file.vo.UploadReturnVO;
import com.okbihuo.perm.core.mp.base.MyController;
import com.okbihuo.perm.core.mp.base.MyService;
import com.okbihuo.perm.core.tool.api.BaseException;
import com.okbihuo.perm.core.tool.api.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Map;

/**
 * 文件controller
 * @author stt
 */
@RestController
@RequestMapping("file")
@RequiredArgsConstructor
public class ComFileController extends MyController<ComFile> {

    private final ComFileService fileService;
    private final FileLogic fileLogic;
    private final FileConfig fileConfig;

    @Override
    protected MyService<ComFile> getService() {
        return fileService;
    }

    /**
     * 上传文件
     * @param file 文件内容
     * @return 文件上传结果
     */
    @RequestMapping("upload")
    public String uploadFiles(@RequestParam("file") MultipartFile file,
                                              @RequestParam(defaultValue = "") String dir,
                                              final HttpServletResponse response) throws BaseException {
        response.setHeader("X-Frame-Options", "SAMEORIGIN");
        ComFile upload = fileLogic.upload(file, dir);
        // 获取访问地址
        UploadReturnVO uploadReturnVO = new UploadReturnVO();
        uploadReturnVO.setDir(dir);
        uploadReturnVO.setName(upload.getOriginalName());
        uploadReturnVO.setShortUrl(upload.getId()+"");
        // 返回永久可用地址
        uploadReturnVO.setUrl(fileConfig.getDownloadPrefix()+upload.getId());
        return R.data(uploadReturnVO).toJSON();
    }

    /**
     * 下载文件
     * @param id 文件id
     * @return 谷歌浏览器会开启下载
     */
    @RequestMapping(value="download/{id}",method= RequestMethod.GET)
    public void downloadFiles(@PathVariable("id")Integer id, HttpServletResponse response) throws BaseException {
        fileLogic.download(id,true,response);
    }

    /**
     * 预览图片（google打开文件是直接预览）
     * @param id 文件id
     * @return 如果是图片，谷歌浏览器会预览，否则直接进入下载
     */
    @RequestMapping(value="preview/{id}",method= RequestMethod.GET)
    public void previewFiles (@PathVariable("id")Integer id,
                                                HttpServletResponse response) throws BaseException {
        fileLogic.download(id,false,response);
    }

    /**
     * 获取文件url（google打开文件是直接预览）
     * @param id 文件id
     * @return 文件展示url
     */
    @RequestMapping(value="getUrl/{id}",method= RequestMethod.GET)
    public String previewFiles (@PathVariable("id")Long id) throws BaseException {
        String fileUrl = fileLogic.getFileUrl(id);
        return R.data(Collections.singletonMap("location",fileUrl)).toJSON();
    }

    /**
     * TinyMCE上传文件
     * @param file 文件内容
     * @return 格式必须为 { location : "/demo/image/1.jpg" }
     */
    @RequestMapping("uploadForTinyMCE")
    public String uploadForTinyMce(@RequestParam("file") MultipartFile file,
                                                   @RequestParam(defaultValue = "/tinyMCE") String dir,
                                                   final HttpServletResponse response) throws BaseException {
        response.setHeader("X-Frame-Options", "SAMEORIGIN");
        ComFile comFile = fileLogic.upload(file, dir);
        String fileUrl = fileLogic.getFileUrl(comFile.getId());
        Map<String, String> map = Collections.singletonMap("location", fileUrl);
        return R.data(map).toJSON();
    }
}
